<?php

namespace App\Http\Controllers\Backend\User;

use App\Repository\Models\Shop;
use Illuminate\Http\Request;
use App\Http\Controllers\Backend\BaseController;
use App\Repository\Repositories\Interfaces\UserRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\ConstDir\ErrorConst;
use Illuminate\Support\Facades\DB;


/**
 * 用户管理
 *
 * @package App\Http\Controllers\Backend
 */
class UserController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    protected $user_repo;

    public function __construct(UserRepository $user_repo)
    {
        $this->user_repo = $user_repo;
    }


    public function index()
    {
        $user = $this->user_repo->with(['company'=>function($query){
            $query->select(['user_id', 'id','company_name','angent','step']);
        },'company_material'])->paginate($this->perPage,['id','phone','name','created_at','identification_type','personal_enterprise']);

//        dd($user);
        return $this->success(ErrorConst::SUCCESS_CODE, $user,true);

    }


    public function show($id)
    {

        $user_info = $this->user_repo->with(['company','company_material','userInvoice','address','orders'])->find($id,['id','name','phone','status','identification_type','created_at','is_city_partner','is_test_user','personal_enterprise','shop_id']);

        if(!empty($user_info['data']['shop_id'])){
            $shop=Shop::query()->where('sp_id',$user_info['data']['shop_id'])->get()->toArray();
            if($shop){
                $shop[0]['sp_type_id']=explode(',',$shop[0]['sp_type_id']);
                $user_info['data']['shop']=$shop[0];
            }else{
                $user_info['data']['shop']=[];
            }

        }
        // 各种订单计数
        $count = [
            'normal'=>0, // 普通商品订单
            'item'=>0, // 库品订单
            'match' =>0, // 配单
            'off'=>0, // 折扣订单
            'service'=>0, // 资质服务订单
        ];
        array_walk($user_info['data']['orders'], function ($value, $key) use (&$count) {
            if ($value['order_type']==1){
                $count['normal']++;
            }elseif($value['order_type']==2){
                $count['item']++;
            }elseif($value['order_type']==4){
                $count['match']++;
            }elseif($value['order_type']==5){
                $count['off']++;
            }elseif($value['order_type']==6){
                $count['service']++;
            }
        });
        $user_info['data']['count'] = $count;
        return $this->success(ErrorConst::SUCCESS_CODE, $user_info);
    }



    public function update(Request $request , $id)
    {

        $this->validate($request, [
            'update_type' => 'required'
        ]);
        $data = $request->all();

        //查询相应用户信息
        $userinfo = $this->user_repo->find($id);
        $shop = Shop::query()->where('sp_id',$userinfo['data']['shop_id'])->get()->toArray();
        $shop= isset($shop[0]) ? $shop[0] : null;

        if($data['update_type'] == 'update_status'){//用户状态

            $value['status'] = $userinfo['data']['status'] == 0 ? 1 : 0;
            $ret = $this->user_repo->update($value, $id);

        }elseif ($data['update_type'] == 'update_partner') {//是否城市合伙人

            $value['is_city_partner'] = $userinfo['data']['is_city_partner'] == 0 ? 1 : 0;
            $ret = $this->user_repo->update($value, $id);

        }elseif ($data['update_type'] == 'update_test_user') {//是否内测人员

            $value['is_test_user'] = $userinfo['data']['is_test_user'] == 0 ? 1 : 0;
            $ret = $this->user_repo->update($value, $id);

        }elseif ($data['update_type'] == 'status1') {//是否商铺开启

            $value['sp_status'] = $shop['sp_status'] == 0 ? 1 : 0;
            $ret = Shop::query()->where('sp_id',$userinfo['data']['shop_id'])->update($value);

        }

        if ($ret) {
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, ErrorConst::SUCCESS_CODE);
        } else {
            return $this->error(ErrorConst::SUCCESS_CODE);
        }
    }




}
